﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
//using Microsoft.Office.Interop.Word;
using System.IO;
namespace TS_XY_Train_Repaire.UserCon.Main_Base
{
    public partial class uc_Main_Base_UploadWord : DevExpress.XtraEditors.XtraForm
    {
        public string strLoadID = "";
        public uc_Main_Base_UploadWord()
        {
            InitializeComponent();
        }

        private void uc_Main_Base_UploadWord_Load(object sender, EventArgs e)
        {
            if (strLoadID != "")
            {
                try
                {
                    if (!Directory.Exists(TS_Win_Class._EXP_Direct))
                    {

                        Directory.CreateDirectory(TS_Win_Class._EXP_Direct);

                    }
                    if (File.Exists(TS_Win_Class._EXP_Direct + "Temp.pdf"))
                    {
                        File.Delete(TS_Win_Class._EXP_Direct + "Temp.pdf");
                    }
                    System.Data.DataTable dt = TS_Win_Class.GetFilledDataTable("select top 1 * from TS_S_T_P_Tree_Resource where f_del=0 and f_mainid= " + strLoadID);
                    string file = dt.Rows[0]["f_brief"].ToString();
                    byte[] byteTo = Convert.FromBase64String(file);
                    string mTransRtf = System.Text.Encoding.Default.GetString(byteTo);
                    string fileName = "";
                    fileName = TS_Win_Class._EXP_Direct + "Temp.pdf";
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(byteTo);
                    FileStream fs = new FileStream(fileName, FileMode.CreateNew);
                    BinaryWriter bw = new BinaryWriter(fs);

                    bw.Write(byteTo, 0, byteTo.Length);
                    bw.Close();
                    fs.Close();

                    PDF_Main.LoadFile(TS_Win_Class._EXP_Direct + "Temp.pdf");
                }
                catch
                {
                    PDF_Main.LoadFile(null);
                }
            }
        }

        private void btn_liulan_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.Filter = "PDF文档(*.pdf)|*.pdf";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    PDF_Pre.LoadFile(openFileDialog1.FileName);
                }
            }
            catch
            {

            }
        }

        private void btn_Upload_Click(object sender, EventArgs e)
        {
            try
            {
                string FileName = openFileDialog1.FileName;
                if (FileName != "")
                {
                    string sql = string.Empty;
                    FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                    byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                    string base64StringDOC = Convert.ToBase64String(bt);
                    br.Dispose();
                    fs.Dispose();
                    if ((TS_Win_Class.GetFilledDataTable("select * from TS_S_T_P_Tree_Resource where f_del=0 and f_mainid=" + strLoadID).Rows.Count == 0))
                    {
                        sql = "insert into  TS_S_T_P_Tree_Resource (f_mainid,f_filename,f_brief,f_type,f_ftype) values(' " + strLoadID + "','" + openFileDialog1.SafeFileName + "','" + base64StringDOC + "',3,0)";

                    }
                    else
                    {
                        sql = "UPDATE TS_S_T_P_Tree_Resource SET f_filename='" + openFileDialog1.SafeFileName + "',f_brief='" + base64StringDOC + "' where f_del=0 and f_mainid=" + strLoadID;
                    }
                    TS_Win_Class.MySqlCom(sql);
                    MessageBox.Show("上传成功！");
                    this.DialogResult = DialogResult.Cancel;
                }
                else
                {
                    MessageBox.Show("请选择需要上传的文档！");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}